package core

import (
	"fmt"
	"strconv"
	"strings"
)

var TimerStrNULL = "00:00:00"

type TimerStr struct { }

func (t TimerStr) SumTimers(v, sep string) int {
	a := strings.Split(v, sep)
	if len(a) == 0 {
		return 0
	}
	var sum int
	for i := 0; i < len(a); i++ {
		sum += t.SecondAdd(a[i])
	}
	return sum
}

func (t TimerStr) SumAndResolveTimers(v, sep string) string {
	sum := t.SumTimers(v, sep)
	if sum == 0 {
		return TimerStrNULL
	}
	return ResolveTime(sum)
}
// 平均时间
func (t TimerStr) AvgTimers (v,sep string, num int) ( string, string) {
	// 计算排版
	sum := t.SumTimers(v, sep)
	if sum == 0 {
		return TimerStrNULL, TimerStrNULL
	}

	return ResolveTime(sum), ResolveTime(sum / num)
}
// 秒转化为时间单位输出
func (t TimerStr) SecondAdd(a string) (sum int){
	arr := strings.Split(a, ":")

	if len(arr) < 3 {
		fmt.Println("存在问题数据 : ", a)
		return
	}

	// 时
	hour,_ := strconv.Atoi(arr[0])
	if hour > 0 {
		sum +=  hour * 60 * 60 // 换算为秒
	}
	// 分
	minute,_ := strconv.Atoi(arr[1])
	if minute > 0 {
		sum += minute  * 60 // 换算为秒
	}
	// 秒
	second,_ := strconv.Atoi(arr[2])
	if second > 0 {
		sum += second // 换算为秒
	}
	return
}

func ResolveTime(second int) (ret string) {
	ret = ret + strconv.Itoa( second / 60 / 60)
	// 分
	minute := second % (60 * 60)
	ret = ret + ":" + strconv.Itoa(minute / 60 )
	// 秒
	ret = ret + ":" + strconv.Itoa(minute % (60 ))
	return
}